//
//  BJShoppingOnlineCollectionViewController.m
//  Dome
//
//  Created by 郭松伟 on 16/5/13.
//  Copyright © 2016年 Dxy. All rights reserved.
//

#import "BJShoppingOnlineCollectionViewController.h"

@interface BJShoppingOnlineCollectionViewController ()<UISearchControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *subTableView;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) UISearchBar *searchBar;

@end

@implementation BJShoppingOnlineCollectionViewController

static NSString *kcellIdentifier = @"collectionCellID";
static NSString *kheaderIdentifier = @"headerIdentifier";
static NSString *kfooterIdentifier = @"footerIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addSearchBar]; // 添加搜索框
    
    // 通过Nib生成cell，然后注册 Nib的view需要继承 UICollectionViewCell
    [self.collectionView registerNib:[UINib nibWithNibName:@"BJShoppingOnlineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];
    
    // 注册headerView Nib的view需要继承UICollectionReusableView
    [self.collectionView registerNib:[UINib nibWithNibName:@"BJShoppingOnlineCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
    
    // 注册footerView Nib的view需要继承UICollectionReusableView
    [self.collectionView registerNib:[UINib nibWithNibName:@"BJShoppingOnlineCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];
    
    self.collectionView.allowsMultipleSelection = YES; // 默认为NO,是否可以多选
    
    self.collectionView.backgroundColor = [UIColor whiteColor];

}

- (void)addSearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.navigationItem.titleView.frame.size.width, 44)];
    
    _searchBar.delegate = self;
    
    self.navigationItem.titleView = _searchBar;
    
    // 搜索框の背景文字
    self.searchBar.placeholder = @"请输入要搜索の内容";
    
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    // 设置显示控制器の代理
    _searchController.delegate = self;
}

#pragma mark - searchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [self.searchDisplayController setActive:YES];  // 显示或隐藏搜索界面
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:NO];
    [searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.frame = CGRectMake(35, 0, self.navigationItem.titleView.frame.size.width, 44);
    [self.searchDisplayController setActive:NO];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

// 定义展示のUICollectionViewCellの个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 6;
}

// 定义展示のSectionの个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

// 每个UICollectionView展示の内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 重用cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];
    // 赋值
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
    UILabel *label = (UILabel *)[cell viewWithTag:2];
    NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];
    imageView.image = [UIImage imageNamed:imageName];
    label.text = imageName;
    
//    cell.backgroundColor = [UIColor redColor];
    
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    NSString *reuseIdentifier;
    
    if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
        reuseIdentifier = kfooterIdentifier;
    } else {
        reuseIdentifier = kheaderIdentifier;
    }
    
    UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
    
    UILabel *label = (UILabel *)[view viewWithTag:1];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        label.text = [NSString stringWithFormat:@"家具家纺:%ld",(long)indexPath.section];
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        view.backgroundColor = [UIColor lightGrayColor];
//        label.text = [NSString stringWithFormat:@"footer:%ld",(long)indexPath.section];
        label.text = [NSString stringWithFormat:@""];
    }
    return view;
}

#pragma mark <UICollectionViewDelegate>

// 定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    return CGSizeMake(60, 80);
    return CGSizeMake((self.view.frame.size.width - 50) / 4, (self.collectionView.frame.size.height - 30) / 5);
}

// 定义每个Section 的 margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);// 分别为上、左、下、右
}

// 返回头headerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size={self.view.frame.size.width, 44};
    return size;
}

// 返回头footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size={self.view.frame.size.width, 10};
    return size;
}
// 每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

// 每个item之间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 100;
//}

// 选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    GoodsSecondViewController *ickImageViewController = [[GoodsSecondViewController alloc] init];
//    
//    if (indexPath.item == 0)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
//    if (indexPath.item == 1)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
//    if (indexPath.item == 2)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
//    if (indexPath.item == 3)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
//    if (indexPath.item == 4)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
//    if (indexPath.item == 5)
//        [self.navigationController pushViewController: ickImageViewController animated:YES];
    
//    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//    [cell setBackgroundColor:[UIColor blueColor]];
//    NSLog(@"选择了%ld.JPG",(long)indexPath.row);
}
// 取消选择了某个cell
//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
//    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//    [cell setBackgroundColor:[UIColor clearColor]];
//    NSLog(@"取消了对%ld.JPGの选择",(long)indexPath.row);
//}

@end
